package com.cci.kangdao.task;
import com.cci.kangdao.crmapi.CRMApiUtils;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.crmapi.CRMServiceConst;
import com.cci.kangdao.dao.SyncServiceTechnicianMsgToCRMTaskDao;
import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;


@Component
public class SyncServiceTechnicianMsgToCRMTask {
	private Logger log = Logger.getLogger(this.getClass().getName());
	@Autowired
	private SyncServiceTechnicianMsgToCRMTaskDao syncServiceTechnicianMsgToCRMTaskDao;
	@Autowired
	private RedisClientUtils redisClientUtils;


	//同步失败的服务技师数据    半小时同步一次到CRM
    public void halfHourSyncServiceTechnicianMsgListToCRM(){
		//获取redis中未同步成功的服务技师数据
		List<Map<String, Object>> serviceTechnicianMsgList = redisClientUtils.getObjectList("ServiceTechnicianMsgList");
		if(CollectionUtils.isNotEmpty(serviceTechnicianMsgList)){
			for(Map<String, Object> serviceTechnicianMsg : serviceTechnicianMsgList){
				int syncCount = 0;  //同步次数
				if(serviceTechnicianMsg.containsKey("syncCount")){
					syncCount = Integer.parseInt(serviceTechnicianMsg.get("syncCount").toString());
				}
				syncCount++;
				serviceTechnicianMsg.put("syncCount",syncCount);
				serviceTechnicianMsg.put("company","XCEC");
				serviceTechnicianMsg.put("appUserID","1");
			}

			Iterator <Map<String, Object>> it = serviceTechnicianMsgList.iterator();
			while (((Iterator) it).hasNext()){
				if (Integer.parseInt(it.next().get("syncCount").toString())>5){
					it.remove();
				}
			}
		}


		if(CollectionUtils.isNotEmpty(serviceTechnicianMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",serviceTechnicianMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					redisClientUtils.delkey("ServiceTechnicianMsgList");
					List<Map<String, Object>> serviceMans = new ArrayList<>();
					JSONArray serviceManJSONArray = crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceMans.addAll(analysisCRMData(serviceManJSONArray));
					}
					redisClientUtils.setObjectList("ServiceTechnicianMsgList",serviceMans,0);
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}
	}






	//获取XCEC下所有的服务技师信息传给CRM   只执行一次
    public void getServiceTechnicianMsgList(){
		//获取XCEC授权下所有服务站的服务技师信息
		List<Map<String,Object>> serviceTechnicianMsgList = syncServiceTechnicianMsgToCRMTaskDao.getServiceTechnicianMsgList(4);
		if(CollectionUtils.isNotEmpty(serviceTechnicianMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",serviceTechnicianMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					/*List<Map<String, Object>> serviceMans = new ArrayList<>();
					JSONArray serviceManJSONArray =  crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceMans.addAll(analysisCRMData(serviceManJSONArray));
					}
					if(CollectionUtils.isNotEmpty(serviceMans)){
						redisClientUtils.setObjectList("ServiceTechnicianMsgList",serviceMans,0);
					}*/
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}
	}

	//每天23点同步服务技师信息到CRM   服务器时间早8小时   所以这里定为下午15点执行
    public void syncServiceTechnicianMsgToCRM(){
		//获取redis中未同步成功的服务技师数据
		//List<Map<String, Object>> serviceTechnicianMsgList = redisClientUtils.getObjectList("ServiceTechnicianMsgList");
		//获取当前时间
		String systime = LocalTimeUtil.getRealCurrentTime();
		//获取当前时间的年月日信息
		systime = systime.substring(0,10);
		//获取XCEC每天新增的服务技师信息
		List<Map<String,Object>> newAddServiceTechnicianMsgList = syncServiceTechnicianMsgToCRMTaskDao.getNewAddServiceTechnicianMsgList(4,systime);
		if(CollectionUtils.isNotEmpty(newAddServiceTechnicianMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",newAddServiceTechnicianMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步新增服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					/*JSONArray serviceManJSONArray = crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceTechnicianMsgList.addAll(analysisCRMData(serviceManJSONArray));
					}*/

				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步新增服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}

		log.info("当前时间:"+systime+",XCEC同步新增服务技师信息结束");

		//获取XCEC每天修改过用户信息并且角色是服务技师的数据
		List<Map<String,Object>> updateServiceTechnicianUserMsgList = syncServiceTechnicianMsgToCRMTaskDao.getUpdateServiceTechnicianUserMsgList(4,systime);
		if(CollectionUtils.isNotEmpty(updateServiceTechnicianUserMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",updateServiceTechnicianUserMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步修改服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					/*JSONArray serviceManJSONArray = crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceTechnicianMsgList.addAll(analysisCRMData(serviceManJSONArray));
					}*/
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步修改服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}
		log.info("当前时间:"+systime+",XCEC同步修改服务技师信息结束");

		//获取XCEC角色和用户表中删除服务技师数据
		List<Map<String,Object>> deleteUserHasRolesMsgList = syncServiceTechnicianMsgToCRMTaskDao.getDeleteUserHasRolesMsgList(4,systime);
		if(CollectionUtils.isNotEmpty(deleteUserHasRolesMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",deleteUserHasRolesMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步作废角色服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					/*JSONArray serviceManJSONArray = crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceTechnicianMsgList.addAll(analysisCRMData(serviceManJSONArray));
					}*/
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步作废角色服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}
		log.info("当前时间:"+systime+",XCEC同步删除服务技师信息结束");

		//获取XCEC用户作废并且角色是服务技师的用户
		List<Map<String,Object>> deleteUserMsgList = syncServiceTechnicianMsgToCRMTaskDao.getDeleteUserMsgList(4,systime);
		if(CollectionUtils.isNotEmpty(deleteUserMsgList)){
			try {
				CRMResponeEntity crmResponeEntity = CRMApiUtils.getCRMServiceApi().syncServiceStationUser("XCEC",deleteUserMsgList);
				if(crmResponeEntity.getStatus() != 0) {
					log.error("同步删除服务技师信息至CRM失败!状态:" + crmResponeEntity.getStatus() + ",错误信息:" + crmResponeEntity.getMessage());
					/*JSONArray serviceManJSONArray = crmResponeEntity.getData().getJSONArray("ServiceMans");
					if(CollectionUtils.isNotEmpty(analysisCRMData(serviceManJSONArray))){
						serviceTechnicianMsgList.addAll(analysisCRMData(serviceManJSONArray));
					}*/
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("同步删除服务技师信息至CRM失败!错误信息:" + e.getMessage(),e);
			}
		}
		log.info("当前时间:"+systime+",XCEC同步作废服务技师信息结束");

		//redisClientUtils.delkey("ServiceTechnicianMsgList");
		//redisClientUtils.setObjectList("ServiceTechnicianMsgList",serviceTechnicianMsgList,0);
	}


	//解析CRM数据并返回List<Map<String,Object>>数据
	private List<Map<String,Object>> analysisCRMData(JSONArray serviceManJSONArray){
		List<Map<String, Object>> serviceMans = new ArrayList<>();
		if(serviceManJSONArray.size()>0){
			for(int i = 0;i<serviceManJSONArray.size();i++){
				JSONObject job = serviceManJSONArray.getJSONObject(i);
				Map<String,Object> serviceManMap = new HashMap<>();
				serviceManMap.put("syncCount",job.get("syncCount"));
				serviceManMap.put("stationNo",job.get("stationNo"));
				serviceManMap.put("requstStatus",job.get("requstStatus"));
				serviceManMap.put("position",job.get("position"));
				serviceManMap.put("phone",job.get("phone"));
				serviceManMap.put("contactName",job.get("contactName"));
				serviceManMap.put("errorMsg",job.get("errorMsg"));
				serviceManMap.put("company","XCEC");
				serviceManMap.put("appUserID",1);
				serviceMans.add(serviceManMap);
			}
		}
		return serviceMans;
	}

}
